﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DoAnOOP
{

    public delegate void MyEventHandler(object sender, MyEventArgs e);
    public partial class TextBoxEdit : UserControl
    {
        public TextBoxEdit()
        {
            InitializeComponent();
            ChoPhepDoi = true;
        }
        [Category("Data"), Description("Text cua TextboxEdit")]
        public string Text
        {
            get { return textBox1.Text; }
            set { this.textBox1.Text = value; }
        }
        private string backup;
        public static bool ChoPhepDoi = true;
        private void pictureBox1_Click(object sender, EventArgs e)  // Nút edit - Hiện Ok and Cancel
        {
            pictureBox2.Visible = true;
            pictureBox3.Visible = true;
            this.textBox1.ReadOnly = false;
            this.textBox1.BackColor = Color.Wheat;
            this.textBox1.Focus();
            backup = textBox1.Text;
            ChoPhepDoi = false;
        }

        private void TextBoxEdit_MouseEnter(object sender, EventArgs e) // Hiện edit khi di chuột vào TextBoxEdit
        {
            if (ChoPhepDoi)
            {
                Graphics gra = this.pictureBox1.CreateGraphics();
                pictureBox1.Visible = true;
            }
        }

        private void TextBoxEdit_MouseLeave(object sender, EventArgs e) // Mất edit khi di chuột ra khỏ TextBoxEdit
        {
            this.pictureBox1.Visible = false;
        }

        private void textBox1_MouseEnter(object sender, EventArgs e)    // Hiện edit khi di chuột vào textBox
        {
            if (ChoPhepDoi)
            {
                Graphics gra = this.pictureBox1.CreateGraphics();
                pictureBox1.Visible = true;
            }
        }

        private void pictureBox1_MouseEnter(object sender, EventArgs e) // Hiện edit khi di chuột vào edit :))
        {
            if (ChoPhepDoi)
            {
                Graphics gra = this.pictureBox1.CreateGraphics();
                pictureBox1.Visible = true;
            }
        }

        private void pictureBox3_Click(object sender, EventArgs e)  // Nút cancel
        {
            ChoPhepDoi = true;
            textBox1.Text = backup;
            pictureBox2.Visible = false;
            pictureBox3.Visible = false;
            textBox1.ReadOnly = true;
            textBox1.BackColor = SystemColors.Control;
        }

        private void pictureBox2_Click(object sender, EventArgs e)  // Send picturebox, e MouseEventArd
        {
            OnMyEvent(new MyEventArgs());
        }

        public void Refresh()
        {
            pictureBox1.Visible = false;
            pictureBox2.Visible = false;
            pictureBox3.Visible = false;
            textBox1.ReadOnly = true;
            textBox1.BackColor = SystemColors.Control;
            ChoPhepDoi = true;
        }

        public event MyEventHandler MyEvent;
        protected virtual void OnMyEvent(MyEventArgs e)
        {
            MyEvent(this, e);
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                pictureBox2_Click(sender, e);
            }
            else if (e.KeyChar == 27)
            {
                pictureBox3_Click(sender, e);
            }
        }

    }
    public class MyEventArgs : EventArgs
    {
    }
}
